<?php

class Talktangle_TalktangleController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
    $this->view->someVar = 'someVal';
  }

  public function viewAction()
  {
    $talktangle_id = $this->_getParam('id');
     
    $this->view->form = $form = new Talktangle_Form_Talktangle_View();    

    $talktangle = Engine_Api::_()->getItem('talktangle', $talktangle_id);
    
    $form->populate(array('title'=>$talktangle->content));
        
    $this->view->preview = $talktangle->preview;
    
    if(!empty($talktangle->promotion_id)){
        $promotion = Engine_Api::_()->getItem('talktangle_promotion', $talktangle_id->promotion_id);
        $this->view->pro_title = $promotion->title;
        $this->view->pro_description = $promotion->description;
    }
    if(!empty($talktangle->ad_id)){
        $promotion = Engine_Api::_()->getItem('talktangle_ad', $talktangle_id->ad_id);
        $this->view->pro_title = $promotion->title;
        $this->view->pro_description = $promotion->description;
        $this->view->pro_image = $promotion->photo_id;
    }
    // Check post
    if(!$this->getRequest()->isPost()){
        return;
    }
    $this->view->isValidF = true;
    // Check form validate
    if(!$form->isValid($this->getRequest()->getPost())){
        $this->view->isValidF = false;
        return;
    }
    
    $values = $form->getValues(); 

    $table = Engine_Api::_()->getDbTable('talktangles','talktangle');
    
  }
  
  public function createAction(){
	
    if( !$this->_helper->requireUser()->isValid() ) {
		return;
	}
     $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->form = $form = new Talktangle_Form_Talktangle_Create();

    // Check post
    if(!$this->getRequest()->isPost()){
        return;
    }
    $this->view->isValidF = true;
    // Check form validate
    if(!$form->isValid($this->getRequest()->getPost())){
        $this->view->isValidF = false;
        return;
    }

    $values = $form->getValues(); 

    $table = Engine_Api::_()->getDbTable('talktangles','talktangle');

    //save form values
    $db = $table->getAdapter();
    $db->beginTransaction();
    try
    {            
        $talktangle = $table->createRow();
        $talktangle->setFromArray($values);
        $talktangle->owner_id = $viewer->getIdentity();
        $talktangle->type = $viewer->type;
        $talktangle->duration = 'minute';
        $talktangle->save();
        $db->commit();   
               
        $form->addNotice($this->view->translate('Your talktangle created successfully.'));                  
        
    }
    catch(Exception $e)
    {
        $db->rollBack();
        throw $e;
    }  
    
    //copmany type
    //Add adv, promotion, duration
    if($viewer->type == 'company'){
        
        //add value to promotion
        $promotiontable = Engine_Api::_()->getDbTable('promotions','talktangle');
        //save form values
        $db = $promotiontable->getAdapter();
        $db->beginTransaction();
        try
        {            
            $promotion = $promotiontable->createRow();
            $promotion->owner_id = $viewer->getIdentity();
            
            if($values['pro_title']){
                $promotion->title = $values['pro_title'];          
            } 
            if($values['pro_description']){
                $promotion->description = $values['pro_description'];         
            }             
            $promotion->save();  
            
            //update promotion_id in talktangle
            $talktangle->promotion_id = $promotion->promotion_id;
            $talktangle->save();
                      
            $db->commit();   
        }
        catch(Exception $e)
        {
            $db->rollBack();
            throw $e;
        }    
        
        //add value to ads
        $adsTable = Engine_Api::_()->getDbTable('ads','talktangle');
        //save form values
        $db = $promotiontable->getAdapter();
        $db->beginTransaction();
        try
        {                     
            $ads = $adsTable->createRow();
            
            $ads->owner_id = $viewer->getIdentity();
            if($values['ads_title']){
                $ads->title = $values['ads_title'];      
            }    
            if($values['ads_description']){
                $ads->description = $values['ads_description'];           
            }                  
                  
            if(!empty($values['file_id'])){
                $ads->photo_id = $values['file_id'];          
            }     
        
            $ads->save(); 
            
            //update ads_id in talktangle
            $talktangle->ad_id = $ads->ad_id;
            $talktangle->save();
                        
            $db->commit();      
            return $this->continueProcessBuy($ads, $form);
        }          
        catch(Exception $e)
        {
            $db->rollBack();
            throw $e;
        }            
    }                       
  }
  
  public function adsimageAction(){
        if(!$this->getRequest()->isPost()) return;
        $table = Engine_Api::_()->getDbTable('ads','talktangle'); 
        $file_id = $table->setPhoto($_FILES['ads_image']); 
        $type = 'thumb.normal';
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id, $type);
            if( !$file ) {
                return ;
            } 
        //get normal photo to save           
        $file_url = $file->map();
        $this->view->file_url = $file_url;
        $this->view->file_id = $file_id;
        
        //get full size photo to preview
        $preview = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id);
        $preview_url = $preview->map();
        $this->view->preview_url = $preview_url;                
  }

  public function promotionimageAction(){
        if(!$this->getRequest()->isPost()) return;
        $table = Engine_Api::_()->getDbTable('promotions','talktangle'); 
        $file_id = $table->setPhoto($_FILES['pro_image']); 
        $type = 'thumb.normal';
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id, $type);
            if( !$file ) {
                return ;
            } 
        //get normal photo to save           
        $file_url = $file->map();
        $this->view->file_url = $file_url;
        $this->view->file_id = $file_id;
        
        //get full size photo to preview
        $preview = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id);
        $preview_url = $preview->map();
        $this->view->preview_url = $preview_url;                
  }
  
  public function paypalreturnAction(){

        $state = $this->_getParam('state');
        $order_id = $this->_getParam('order_id');
        $tangle_id = $this->_getParam('tangle_id');
        $type = $this->_getParam('type');
        
        $order = Engine_Api::_()->getItem('payment_order',$order_id);
        $tangle = Engine_Api::_()->getItem($type,$tangle_id);             
        if($state == 'return'){

            // Process
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();       
            try
            {            
                //change order status
                $order->state = 'completed';
                $order->save();
                //tangle status
                $tangle->payment_status = 2; //payment complete
                $tangle->save();    
                $db->commit();                   
            }
            catch(Exception $e)
            {
                $db->rollBack();
                throw $e;
            }             
            $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();             
            $actionT = Engine_Api::_()->getDbtable('actions','activity');
            $actionT->addActivity($viewer, $tangle ,'post_talktangle');
          
            return $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'index'), 'talktangle_extended', true);
        }
        elseif($state == 'cancel'){
  
            // Process
            $db = Engine_Db_Table::getDefaultAdapter();
            $db->beginTransaction();       
            try
            {            
                //tangle status
                $tangle->payment_status = 1;  //payment pending    
                //change ads id
                $tangle->ad_id = 0;  
                $tangle->save();    
                $db->commit();      
              
            }
            catch(Exception $e)
            {
                $db->rollBack();
                throw $e;
            }                      
            //return create form
            if($type=='talktangle'){
                return $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'index','type'=>'createtalk','tangle'=>$tangle_id), 'talktangle_extended', true);
            }
            
        }
  }
}
